﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using DWEIP.Common;
using DataWin.Core.ORM;
using DataWin.Core.Model;
using DataWin.Core.Util;
using DWEIP.Models.OA;
using DWEIP.Locale;
using DWEIP.BLL.Core;

namespace DWEIP.BLL.OA
{
    public partial class OA_CalBLL : BasicBLL
    {
        #region DAL
        public ncal001Model DAL_GetById(int Pk_Id)
        {
            Table _ncal001 = new Table("ncal001");
            _ncal001.SQL.Value = @"
SELECT * FROM ncal001 Where pk_id = @id";
            _ncal001.Params.Clear();
            _ncal001.Params.Add("id", new Data(Pk_Id));

            BLLSession.Load(_ncal001);

            var msgModelList = (from r in _ncal001
                                select new ncal001Model
                                {
                                    recid = r["recid"].AsString,
                                    pk_id = r["pk_id"].AsInt,
                                    create_user = r["create_user"].AsString,
                                    create_date = r["create_date"].AsDateTime,
                                    modify_user = r["modify_user"].AsString,
                                    modify_date = r["modify_date"].AsDateTime,
                                    last_user = r["last_user"].AsString,
                                    last_date = r["last_date"].AsDateTime,
                                    doc_status = r["doc_status"].AsString,
                                    ref_browse = r["ref_browse"].AsString,
                                    function_id = r["function_id"].AsString,
                                    create_by = r["create_by"].AsString,
                                    is_main = r["is_main"].AsString,
                                    main_id = r["main_id"].AsString,
                                    user_no = r["user_no"].AsString,
                                    cal_confirm = r["cal_confirm"].AsString,
                                    is_transferm = r["is_transferm"].AsString,
                                    cal_date = r["cal_date"].AsString,
                                    time_def = r["time_def"].AsString,
                                    start_time = r["start_time"].AsString,
                                    end_time = r["end_time"].AsString,
                                    cal_type = r["cal_type"].AsString,
                                    title = r["title"].AsString,
                                    attn_desc = r["attn_desc"].AsString,
                                    content = r["content"].AsString,
                                    priority = r["priority"].AsString,
                                    cycle_id = r["cycle_id"].AsString,
                                    is_multiperson = r["is_multiperson"].AsString,
                                    is_hide = r["is_hide"].AsString,
                                    clock_remind = r["clock_remind"].AsString,
                                    visit_type = r["visit_type"].AsString,
                                    visit_no = r["visit_no"].AsString,
                                    visit_status = r["visit_status"].AsString,
                                    negotiation = r["negotiation"].AsString
                                }).ToList();

            return msgModelList[0];
        }


        public IQueryable<SysCalendarPlan> DAL_GetList(Expression<Func<SysCalendarPlan, bool>> whereLambda)
        {
            Table _ncal001 = new Table("ncal001");
            _ncal001.SQL.Value = @"
select recid, pk_id, title, negotiation,
convert(datetime, cal_date+' '+substring(start_time,1,2)+':'+substring(start_time,3,2)) BeginDate,
convert(datetime, cal_date+' '+substring(end_time,1,2)+':'+substring(end_time,3,2)) EndDate,
create_date, '#ff6600' Color,'#ffffff' TextColor,user_no,'true' Editable
from ncal001 ";
            BLLSession.Load(_ncal001);

            var msgModelList = (from r in _ncal001
                                select new SysCalendarPlan
                                {
                                    Id = r["recid"].AsString,
                                    Title = r["title"].AsString,
                                    PlanContent = r["negotiation"].AsString,
                                    BeginDate = r["BeginDate"].AsDateTime,
                                    EndDate = r["EndDate"].AsDateTime,
                                    CreateTime = r["create_date"].AsDateTime,
                                    Url = "",
                                    Color = r["Color"].AsString,
                                    TextColor = r["TextColor"].AsString,
                                    SysUserId = r["user_no"].AsString,
                                    Editable = r["Editable"].AsString,
                                }).ToList().AsQueryable();

            return msgModelList.Where(whereLambda).AsQueryable();
        }

        public bool DAL_Create(ncal001Model model)
        {
            string sqltxt = @"INSERT INTO ncal001 
recid,
--pk_id,
create_user,
create_date,
modify_user,
modify_date,
last_user,
last_date,
doc_status,
ref_browse,
function_id,
create_by,
is_main,
main_id,
user_no,
cal_confirm,
is_transferm,
cal_date,
time_def,
start_time,
end_time,
cal_type,
title,
attn_desc,
content,
priority,
cycle_id,
is_multiperson,
is_hide,
clock_remind,
visit_type,
visit_no,
visit_status,
negotiation,
)
VALUES('{0}', '{1}', {2}, '{3}', '{4}', '{5}', {6}, '{7}', '{8}', {9}, '{10}', '{11}', '{12}', '{13}', '{14}', '{15}', '{16}', '{17}', '{18}', '{19}', '{20}', '{21}', '{22}', '{23}', '{24}', '{25}', '{26}', '{27}', '{28}', '{29}', '{30}', '{31}', '{32}')";

            Table _ncal001 = new Table("ncal001");
            _ncal001.SQL.Value = string.Format(sqltxt,
model.recid,
model.create_user,
model.create_date,
model.modify_user,
model.modify_date,
model.last_user,
model.last_date,
model.doc_status,
model.ref_browse,
model.function_id,
model.create_by,
model.is_main,
model.main_id,
model.user_no,
model.cal_confirm,
model.is_transferm,
model.cal_date,
model.time_def,
model.start_time,
model.end_time,
model.cal_type,
model.title,
model.attn_desc,
model.content,
model.priority,
model.cycle_id,
model.is_multiperson,
model.is_hide,
model.clock_remind,
model.visit_type,
model.visit_no,
model.visit_status,
model.negotiation
                );

            int result = BLLSession.Execute(_ncal001, System.Data.CommandType.Text);
            return result > 0;
        }

        #endregion DAL

        public List<SysCalendarPlan> GetList(ref GridPager pager, DateTime start, DateTime end, string userId)
        {

            IQueryable<SysCalendarPlan> queryData = null;

            queryData = DAL_GetList(a => a.BeginDate >= start && a.EndDate <= end && a.SysUserId == userId);

            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public List<SysCalendarPlan> CreateModelList(ref IQueryable<SysCalendarPlan> queryData)
        {

            List<SysCalendarPlan> modelList = (from r in queryData
                                                    select new SysCalendarPlan
                                                    {
                                                        Id = r.Id,
                                                        Title = r.Title,
                                                        PlanContent = r.PlanContent,
                                                        BeginDate = r.BeginDate,
                                                        EndDate = r.EndDate,
                                                        Color = r.Color,
                                                        TextColor = r.TextColor,
                                                        Url = r.Url,
                                                        SysUserId = r.SysUserId,
                                                        CreateTime = r.CreateTime,
                                                    }).ToList();

            return modelList;
        }

        public bool Create(ref ValidationErrors errors, SysCalendarPlan model)
        {
            try
            {
                ncal001Model entity = DAL_GetById(model.pk_id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                /*
                entity.recid = model.recid;
                entity.pk_id = model.pk_id;
                entity.create_user = model.create_user;
                entity.create_date = model.create_date;
                entity.modify_user = model.modify_user;
                entity.modify_date = model.modify_date;
                entity.last_user = model.last_user;
                entity.last_date = model.last_date;
                entity.doc_status = model.doc_status;
                entity.ref_browse = model.ref_browse;
                entity.function_id = model.function_id;
                entity.create_by = model.create_by;
                entity.is_main = model.is_main;
                entity.main_id = model.main_id;
                entity.user_no = model.user_no;
                entity.cal_confirm = model.cal_confirm;
                entity.is_transferm = model.is_transferm;
                entity.cal_date = model.cal_date;
                entity.time_def = model.time_def;
                entity.start_time = model.start_time;
                entity.end_time = model.end_time;
                entity.cal_type = model.cal_type;
                entity.title = model.title;
                entity.attn_desc = model.attn_desc;
                entity.content = model.content;
                entity.priority = model.priority;
                entity.cycle_id = model.cycle_id;
                entity.is_multiperson = model.is_multiperson;
                entity.is_hide = model.is_hide;
                entity.clock_remind = model.clock_remind;
                entity.visit_type = model.visit_type;
                entity.visit_no = model.visit_no;
                entity.visit_status = model.visit_status;
                entity.negotiation = model.negotiation;
                */
                if (DAL_Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }
    }
}
